package com.lorenzogao.pley.ui.fragment

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lorenzogao.pley.R
import com.lorenzogao.pley.bean.AppInfo
import com.lorenzogao.pley.bean.PageBean
import com.lorenzogao.pley.ui.activity.AppDetailActivity
import com.lorenzogao.pley.ui.adapter.AppInfoAdapter
import com.lorenzogao.pley.ui.base.BaseFragment
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.di.component.DaggerAppInfoComponent
import com.lorenzogao.pley.ui.di.moduel.AppInfoModule
import com.lorenzogao.pley.ui.presenter.AppInfoPresenter
import com.lorenzogao.pley.ui.presenter.contract.AppInfoContract
import com.lorenzogao.pley.ui.widget.DividerItemDecoration
import zlc.season.rxdownload2.RxDownload
import javax.inject.Inject

/**
 * 作者：Lorenzo Gao
 * Date: 2018/1/4
 * Time: 16:58
 * 邮箱：2508719070@qq.com
 * Description:
 */
open abstract class BaseAppInfoFragment : BaseFragment<AppInfoPresenter>(), AppInfoContract.AppInfoView, BaseQuickAdapter.RequestLoadMoreListener {


    private lateinit var mRecyclerView: RecyclerView

    var page: Int = 0

    private lateinit var mAdapter: AppInfoAdapter

    @Inject
    lateinit var mRxDownload: RxDownload

    override fun showResult(appinfo: PageBean<AppInfo>) {

        mAdapter.addData(appinfo.datas)
        if (appinfo.hasMore) {
            page++
        }
        mAdapter.setEnableLoadMore(appinfo.hasMore)
    }


    override fun setLayout(): Int {
        return R.layout.template_recyclerview
    }


    override fun initView() {


        mPresenter.requestData(type(), page)

        initRecyclerView()
    }

    protected fun initRecyclerView() {
        mRecyclerView = getContentView()!!.findViewById(R.id.rl_category)
        mRecyclerView.layoutManager = LinearLayoutManager(activity)

        mRecyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST))

        mAdapter = buildAdapter()

        mAdapter.setOnLoadMoreListener(this)
        mRecyclerView.adapter = mAdapter


        mRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                if (view != null) {
                    mApplication.setView(view)
                }
                var intent = Intent(activity, AppDetailActivity::class.java)
                intent.putExtra("appinfo", mAdapter.getItem(position))
                startActivity(intent)
            }

        })

    }

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerAppInfoComponent.builder().appComponent(appComponent).appInfoModule(AppInfoModule(this))
                .build().injectTopList(this)
    }

    override fun onLoadMoreRequested() {
        mPresenter.requestData(type(), page)
    }

    override fun onLoadMoreComplete() {
        mAdapter.loadMoreComplete()
    }

    abstract fun buildAdapter(): AppInfoAdapter
    abstract fun type(): Int
}